package com.gitee.wsl.android.ui.recyclerView.itemDecoration.builder

import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.TypedValue
import androidx.core.content.ContextCompat

/**
 * @Author : HHotHeart
 * @Time : 2021/5/31 16:07
 * @Description : 线性item分割线修饰类
 */
class LDecoration(private val mContext: Context) : ILDecoration {
    /**
     * 分割线左右内边距（垂直）
     */
    override var leftPadding = 0

    override var rightPadding = 0


    /**
     * 分割线上下内边距（水平）
     */
    override var topPadding = 0

    override var bottomPadding = 0

    /**
     * 分割线颜色或背景
     */
    private var mColor = 0
    private var mDividerDrawable: Drawable? = null

    /**
     * 默认对边界不处理
     */
    private var isDrawLeft: Boolean? = null
    private var isDrawTop: Boolean? = null
    private var isDrawRight: Boolean? = null
    private var isDrawBottom: Boolean? = null

    /**
     * 设置绘制item左上右下属性
     *
     * @param isDrawLeft
     * @param isDrawTop
     * @param isDrawRight
     * @param isDrawBottom
     * @return
     */
    fun setAroundEdge(
        isDrawLeft: Boolean?,
        isDrawTop: Boolean?,
        isDrawRight: Boolean?,
        isDrawBottom: Boolean?
    ): LDecoration {
        this.isDrawLeft = isDrawLeft
        this.isDrawTop = isDrawTop
        this.isDrawRight = isDrawRight
        this.isDrawBottom = isDrawBottom
        return this
    }

    val aroundEdge: Array<Boolean?>
        get() = arrayOf(isDrawLeft, isDrawTop, isDrawRight, isDrawBottom)

    override fun setPadding(dpValuePadding: Float): LDecoration {
        setLeftPadding(dpValuePadding)
        setRightPadding(dpValuePadding)
        setTopPadding(dpValuePadding)
        setBottomPadding(dpValuePadding)
        return this
    }

    override fun setPadding(dimenResId: Int): LDecoration {
        setLeftPadding(dimenResId)
        setRightPadding(dimenResId)
        setTopPadding(dimenResId)
        setBottomPadding(dimenResId)
        return this
    }

    override fun setLeftPadding(dpValuePadding: Float): LDecoration {
        leftPadding =
            DividerHelper.applyDimension(dpValuePadding, TypedValue.COMPLEX_UNIT_DIP).toInt()
        return this
    }

    override fun setRightPadding(dpValuePadding: Float): LDecoration {
        rightPadding =
            DividerHelper.applyDimension(dpValuePadding, TypedValue.COMPLEX_UNIT_DIP).toInt()
        return this
    }

    override fun setLeftPadding(dimenResId: Int): LDecoration {
        leftPadding = Resources.getSystem().getDimensionPixelSize(dimenResId)
        return this
    }

    override fun setRightPadding(dimenResId: Int): LDecoration {
        rightPadding = Resources.getSystem().getDimensionPixelSize(dimenResId)
        return this
    }

    override fun setTopPadding(dpValuePadding: Float): LDecoration {
        topPadding =
            DividerHelper.applyDimension(dpValuePadding, TypedValue.COMPLEX_UNIT_DIP).toInt()
        return this
    }

    override fun setBottomPadding(dpValuePadding: Float): LDecoration {
        bottomPadding =
            DividerHelper.applyDimension(dpValuePadding, TypedValue.COMPLEX_UNIT_DIP).toInt()
        return this
    }

    override fun setTopPadding(dimenResId: Int): LDecoration {
        topPadding = Resources.getSystem().getDimensionPixelSize(dimenResId)
        return this
    }

    override fun setBottomPadding(dimenResId: Int): LDecoration {
        bottomPadding = Resources.getSystem().getDimensionPixelSize(dimenResId)
        return this
    }

    override fun setColorRes(colorResId: Int): LDecoration {
        setColor(ContextCompat.getColor(mContext, colorResId))
        return this
    }

    override fun setColor(color: Int): LDecoration {
        mColor = color
        return this
    }

    override fun setDrawableRes(drawableResId: Int): LDecoration {
        setDrawable(ContextCompat.getDrawable(mContext, drawableResId))
        return this
    }

    override fun setDrawable(drawable: Drawable?): LDecoration {
        mDividerDrawable = drawable
        return this
    }

    //创建Drawable
    override val dividerDrawable: Drawable
        get() {
            //创建Drawable
            if (mDividerDrawable == null) {
                mDividerDrawable = ColorDrawable(mColor)
            }
            return mDividerDrawable!!
        }
}